package com.qianji.controller;

import com.qianji.api.ConsequenceApps;
import com.qianji.pojo.ConsequenceDto;
import com.qianji.service.ConsequenceService;
import com.qianji.utils.Result;
import com.qianji.vo.ConsequenceAddVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * Created by 阳家桂 on 2021/3/12 15:43
 * 对抽奖结果进行操作
 */

@RestController
@Slf4j
@RequestMapping("/consequence/")
public class ConsequenceController {

    @Autowired
    private ConsequenceService consequenceService;

    /**
     * 新增抽奖结果
     * @param consequenceAddVO
     * @return
     */
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public Result addConsequence(@RequestBody ConsequenceAddVO consequenceAddVO){
        consequenceAddVO.setCreateTime(new Date());
        Integer result = consequenceService.addCon(consequenceAddVO);
        if (result>0){
            return Result.success("添加成功",200);
        }else {
            return  Result.failed("添加失败",400);
        }

    }

    /**
     * 删除抽奖结果
     * @param id
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public Result deleteCon(String id){
        Integer result = consequenceService.deleteCon(id);
        if (result>0){
            return Result.success("删除成功",200);
        }else {
            return  Result.failed("删除失败",400);
        }
    }

    /**
     * 根据传入参数查询对应的抽奖结果
     * @param consequenceDto
     * @return
     */
    @RequestMapping(value = "find",method = RequestMethod.POST)
    public Result selectConsequence(@RequestBody ConsequenceDto consequenceDto) {

        log.info("查询抽奖结果条件：{}",consequenceDto);

        return consequenceService.selectCon(consequenceDto);
    }

    /**
     * 领取奖品，将奖品状态改为已领取状态
     * @param id
     * @return
     */
    @RequestMapping(value = "getPrize", method = RequestMethod.POST)
    public Result getPrize(Long id){
        Integer status = consequenceService.getPrize(id);

        if(status == 1){
            return Result.success("领取成功",200);
        }else {
            return Result.failed("领取失败",500);
        }

    }

}
